<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class user_db extends Model{
	public $pk = 'user_id';
	public $table = 'ptx_user';

	private function init_conditions($conditions){
		$conditions_user = NULL;
		if(isset($conditions['keyword'])){
			$keyword = $this->escape($conditions['keyword']);
			$conditions_user .= "AND MATCH (ptx_user.nickname) AGAINST ({$keyword} IN BOOLEAN MODE) OR MATCH (ptx_user.email) AGAINST ({$keyword} IN BOOLEAN MODE) ";
		}
		if(isset($conditions['likekeyword'])){
			$likeword = $this->escape("%{$conditions['likekeyword']}%");
			$conditions_user .= "AND ptx_user.nickname LIKE $likeword OR  ptx_user.email LIKE $likeword ";
		}
		if(isset($conditions['sysid'])){
			$conditions_user .= "AND ptx_user.user_id IN (select ug.user_id from `{$this->dbpre}ptx_user_usergroup` ug where ug.usergroup_id='{$conditions['sysid']}') ";
		}
		if(isset($conditions['memid'])){
			$conditions_user .= "AND ptx_user.user_id IN (select mug.user_id from `{$this->dbpre}ptx_user_usergroup` mug where mug.usergroup_id='{$conditions['memid']}') ";
		}
		
		if(strpos($conditions_user, 'AND') === 0){
			$conditions_user = substr($conditions_user, 3);
		}
		return $conditions_user;
	}

	public function search($conditions=NULL,$page,$pagesize,$fields = null,$sort=null){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_user.total_follower DESC ";
		$fields = isset($fields)?$fields:" ptx_user.* ";
		return $this->pager($page, $pagesize)->findAllJoin($conditions,$sort,$fields);
	}

	public function searchWithShare($conditions=NULL,$page,$pagesize,$fields = null,$sort=null){
		$users = $this->search($conditions,$page,$pagesize,$fields,$sort);
		$sharedb = load_db('share');
		foreach ($users as $key=>$user) {
			$share = $sharedb->find_one(array('user_id'=>$user['user_id']));
			$users[$key]['share']= ($share)?load_api('share')->prepareshare($share):false;
		}
		return $users;
	}

	public function search_no_page($conditions=NULL,$fields = null,$sort=null,$limit){
		if(!$sort)
			$sort = " ptx_user.total_follower DESC ";
		return $this->findAllJoin($conditions,$sort,$fields,$limit);
	}

	public function find_userid_by_uname($name_arr){
		$ret .= is_array($name_arr) ? implode(',', self::quote($name_arr)) : self::quote($name_arr);
		return $this->findAll(" ptx_user.nickname in ({$ret}) ", null ,' ptx_user.user_id,ptx_user.nickname ');
	}

	public function get_user_flashdata($user_id){
		$condition['user_id'] = $user_id;
		$fields = ' ptx_user.usergroup_id,ptx_user.credits,ptx_user.ext_credits_1,ptx_user.ext_credits_2,ptx_user.ext_credits_3 ';
		return $this->find($condition,null,$fields);
	}

	public function checknick($val){
		return false == $this->find(array("nickname"=>$val));
	}

	public function checkemail($val){
		return false == $this->find(array("email"=>$val));
	}

	public function updateUCID($user,$uc_id,$uc_username){
		if($user['user_id']&&!$user['uc_id']){
			if($this->find_by_ucid($uc_id)){
				return;
			}
			$this->update(array('user_id'=>$user['user_id']), array('uc_id'=>$uc_id,'uc_nickname'=>$uc_username));
		}
	}

	public function find_by_ucid($uc_id){
		return $this->find(array('uc_id'=>$uc_id));
	}

	public function create_random_nick($start_name){
		if($this->checknick($start_name)){
			return $start_name;
		}else{
			$start_name .= random(4);
			return $this->create_random_nick($start_name);
		}
	}

	public function update_password_key($to_email,$password_key,$password_expire){
		return $this->update(array('email'=>$to_email), array('lost_password_key'=>$password_key,'lost_password_expire'=>$password_expire));
	}

	public function update_shopuser($user_id,$status=1){
		$condition['user_id'] = $user_id;
		$data['has_store'] = $status;
		return $this->update($condition,$data);
	}

	public function update_staruser($user_id,$status=1){
		$condition['user_id'] = $user_id;
		$data['is_star'] = $status;
		return $this->update($condition,$data);
	}

	function getuser_byid($uid){
		return ($uid)?$this->find(array('user_id'=>$uid)):NULL;
	}

	function getuser_bynick($nick){
		return ($nick)? $this->find(array('nickname'=>$nick)):null;
	}

	function is_banned($uid){
		return ($uid)?$this->find(array('user_id'=>$uid)):false;
	}

	function ban_user($uid){
		return ($uid)? $this->update(array('user_id'=>$uid),array('usergroup_id'=>3)):false;
	}
	public function add_one($data){
		if($this->check_value($data)){
			$data['create_time'] = TIMESTAMP;
			return $this->create($data);
		}
		return false;
	}

	private function check_value($data){
		if(!$data['nickname']){
			return false;
		}
		return true;
	}

	public function add_message($user_id){
		return $this->runSql("UPDATE {$this->tbl_name} SET total_message=total_message+1 WHERE user_id='{$user_id}'");
	}

	public function add_share($user_id){
		$count = load_db('share')->findCount(array('user_id'=>$user_id));
		return $this->runSql("UPDATE {$this->tbl_name} SET total_share={$count} WHERE user_id='{$user_id}'");
	}
	public function del_share($user_id){
		$count = load_db('share')->findCount(array('user_id'=>$user_id));
		return $this->runSql("UPDATE {$this->tbl_name} SET total_share={$count} WHERE user_id='{$user_id}'");
	}
	public function add_album($user_id){
		return $this->incrField(array('user_id'=>$user_id), 'total_album');
	}
	public function del_album($user_id){
		return $this->decrField(array('user_id'=>$user_id), 'total_album');
	}
	public function updateLike($user_id,$count){
		//return $this->incrField(array('user_id'=>$user_id), 'total_like');
		return $this->runSql("UPDATE {$this->tbl_name} SET total_like={$count} WHERE user_id='{$user_id}'");
	}
	public function add_follow($user_id){
		return $this->incrField(array('user_id'=>$user_id), 'total_follow');
	}
	public function remove_follow($user_id){
		return $this->decrField(array('user_id'=>$user_id), 'total_follow');
	}
	public function add_follower($user_id){
		return $this->incrField(array('user_id'=>$user_id), 'total_follower');
	}
	public function remove_follower($user_id){
		return $this->decrField(array('user_id'=>$user_id), 'total_follower');
	}

	public function updateCredits($rule,$user_id){
		$sql = "UPDATE {$this->tbl_name} ".
				"SET ext_credits_1=ext_credits_1+{$rule['ext_credits_1']}, ".
				" ext_credits_2=ext_credits_2+{$rule['ext_credits_2']}, ".
				" ext_credits_3=ext_credits_3+{$rule['ext_credits_3']}, ".
				" credits={$rule['credits']} ".
				"WHERE user_id='{$user_id}'";
		return $this->runSql($sql);
	}
}
